//
//  ViewController.m
//  Lottolandia
//
//  Created by Guido Sabatini on 23/11/13.
//  Copyright (c) 2013 Quelli for Lotto. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, assign) int posX;
@property (nonatomic, strong) NSTimer *moveBackgroundTimer;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.scrollview.contentSize = self.sfondoAzzurro.frame.size;
    self.scrollview.delegate = self;
    [self startHorizontalAnimation:self.nuvola1 withInterval:(self.nuvola1.frame.origin.x/2000.0*40.0)];
    [self startHorizontalAnimation:self.nuvola2 withInterval:(self.nuvola2.frame.origin.x/2000.0*41.0)];
    [self startHorizontalAnimation:self.nuvola3 withInterval:(self.nuvola3.frame.origin.x/2000.0*39.0)];
    [self startHorizontalAnimation:self.nuvola4 withInterval:(self.nuvola4.frame.origin.x/2000.0*42.0)];
    //[self animateTraslationMongolfiera];
    [self animateRotationMongolfiera];
    [self startVerticalAnimation:self.palloncino1 withInterval:(self.palloncino1.frame.origin.y/768.0*20.0)];
    [self startVerticalAnimation:self.palloncino2 withInterval:(self.palloncino2.frame.origin.y/768.0*22.0)];
    [self startVerticalAnimation:self.palloncino3 withInterval:(self.palloncino3.frame.origin.y/768.0*21.0)];
    [self startVerticalAnimation:self.palloncino4 withInterval:(self.palloncino4.frame.origin.y/768.0*19.0)];
    [self startVerticalAnimation:self.palloncino5 withInterval:(self.palloncino5.frame.origin.y/768.0*20.0) inFront:YES];
    [self startVerticalAnimation:self.palloncino6 withInterval:(self.palloncino6.frame.origin.y/768.0*21.0)];
    [self startVerticalAnimation:self.palloncino7 withInterval:(self.palloncino7.frame.origin.y/768.0*18.0)];
    [self startVerticalAnimation:self.palloncino8 withInterval:(self.palloncino8.frame.origin.y/768.0*22.0)];
    [self startVerticalAnimation:self.palloncino9 withInterval:(self.palloncino9.frame.origin.y/768.0*20.0)];
    [self startVerticalAnimation:self.palloncino10 withInterval:(self.palloncino9.frame.origin.y/768.0*20.0)];
    [self startVerticalAnimation:self.palloncino11 withInterval:(self.palloncino9.frame.origin.y/768.0*20.0)];
    
    self.moveBackgroundTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(invalidateTimer)];
    [self.scrollview addGestureRecognizer:tapRecognizer];
}

-(void) invalidateTimer
{
    [self.moveBackgroundTimer invalidate];
}

-(void)viewDidAppear:(BOOL)animated
{
//    [self startHorizontalAnimation:self.nuvola1 withInterval:(self.nuvola1.frame.origin.x/2000.0*40.0)];
//    [self startHorizontalAnimation:self.nuvola2 withInterval:(self.nuvola2.frame.origin.x/2000.0*41.0)];
//    [self startHorizontalAnimation:self.nuvola3 withInterval:(self.nuvola3.frame.origin.x/2000.0*39.0)];
//    [self startHorizontalAnimation:self.nuvola4 withInterval:(self.nuvola4.frame.origin.x/2000.0*42.0)];
//    //[self animateTraslationMongolfiera];
//    [self animateRotationMongolfiera];
//    [self startVerticalAnimation:self.palloncino1 withInterval:(self.palloncino1.frame.origin.y/768.0*20.0)];
//    [self startVerticalAnimation:self.palloncino2 withInterval:(self.palloncino2.frame.origin.y/768.0*22.0)];
//    [self startVerticalAnimation:self.palloncino3 withInterval:(self.palloncino3.frame.origin.y/768.0*21.0)];
//    [self startVerticalAnimation:self.palloncino4 withInterval:(self.palloncino4.frame.origin.y/768.0*19.0)];
//    [self startVerticalAnimation:self.palloncino5 withInterval:(self.palloncino5.frame.origin.y/768.0*20.0) inFront:YES];
//    [self startVerticalAnimation:self.palloncino6 withInterval:(self.palloncino6.frame.origin.y/768.0*21.0)];
//    [self startVerticalAnimation:self.palloncino7 withInterval:(self.palloncino7.frame.origin.y/768.0*18.0)];
//    [self startVerticalAnimation:self.palloncino8 withInterval:(self.palloncino8.frame.origin.y/768.0*22.0)];
//    [self startVerticalAnimation:self.palloncino9 withInterval:(self.palloncino9.frame.origin.y/768.0*20.0)];
//    [self startVerticalAnimation:self.palloncino10 withInterval:(self.palloncino9.frame.origin.y/768.0*20.0)];
//    [self startVerticalAnimation:self.palloncino11 withInterval:(self.palloncino9.frame.origin.y/768.0*20.0)];
    
    [UIView animateWithDuration:3.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        CGRect frame = self.nuvola1Splash.frame;
        frame.origin.x = -2000;
        self.nuvola1Splash.frame = frame;
        //self.nuvola1Splash.alpha = 0.0;
        
        frame = self.nuvola2Splash.frame;
        frame.origin.x = 1200;
        self.nuvola2Splash.frame = frame;
        //self.nuvola2Splash.alpha = 0.0;
    } completion:^(BOOL finished) {
        self.nuvola1Splash.hidden = YES;
        self.nuvola2Splash.hidden = YES;
    }];
    
}

-(void) onTimer {
    // NSLog(@"content offset %f",scrollVw.contentOffset.y);
    if (self.scrollview.contentOffset.x<976) {
        //scroll to desire position
        self.scrollview.contentOffset = CGPointMake(self.scrollview.contentOffset.x+2, 0);
    }
    else
        [self invalidateTimer];
}

-(void)viewWillAppear:(BOOL)animated
{
    CGAffineTransform rotateLeft = CGAffineTransformRotate(CGAffineTransformIdentity, -0.2);
    self.mongolfiera.transform = rotateLeft;
}

-(void) startHorizontalAnimation:(UIImageView*)image withInterval:(int)interval
{
    [UIView animateWithDuration:interval delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        image.frame = CGRectMake(-image.frame.size.width, image.frame.origin.y, image.frame.size.width, image.frame.size.height);
    } completion:^(BOOL finished) {
        image.frame = CGRectMake(self.sfondoAzzurro.frame.size.width+image.frame.size.width, image.frame.origin.y, image.frame.size.width, image.frame.size.height);
        [self startHorizontalAnimation:image withInterval:(40+arc4random()%3)];
    }];
}

-(void) startVerticalAnimation:(UIImageView*)image withInterval:(int)interval
{
    [self startVerticalAnimation:image withInterval:interval inFront:NO];
}

-(void) startVerticalAnimation:(UIImageView*)image withInterval:(int)interval inFront:(BOOL)isInFront
{
    [UIView animateWithDuration:interval delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        image.frame = CGRectMake(image.frame.origin.x, -image.frame.size.height, image.frame.size.width, image.frame.size.height);
    } completion:^(BOOL finished) {
        if (isInFront)
            image.frame = CGRectMake(image.frame.origin.x, self.sfondoAzzurro.frame.size.height+image.frame.size.height, image.frame.size.width, image.frame.size.height);
        else
            image.frame = CGRectMake(image.frame.origin.x, self.sfondoAzzurro.frame.size.height-image.frame.size.height, image.frame.size.width, image.frame.size.height);
        [self startVerticalAnimation:image withInterval:(28+arc4random()%4) inFront:isInFront];
    }];
}

-(void) animateRotationMongolfiera
{
    CGAffineTransform rotateRight  = CGAffineTransformRotate(CGAffineTransformIdentity, 0.2);
    CGAffineTransform rotateLeft = CGAffineTransformRotate(CGAffineTransformIdentity, -0.2);
    
    [UIView animateWithDuration:5 animations:^{
        self.mongolfiera.transform = rotateRight;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:5 animations:^{
            self.mongolfiera.transform = rotateLeft;
        } completion:^(BOOL finished) {
            [self animateRotationMongolfiera];
        }];
    }];
}

-(void) animateTraslationMongolfiera
{
    [UIView animateWithDuration:8 animations:^{
        self.mongolfiera.frame = CGRectMake(self.mongolfiera.frame.origin.x-100+arc4random()%200, self.mongolfiera.frame.origin.y-15+arc4random()%30, self.mongolfiera.frame.size.width, self.mongolfiera.frame.size.height);
    } completion:^(BOOL finished) {
        [self animateTraslationMongolfiera];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.posX > scrollView.contentOffset.x+2)
    {
        self.nuvoleSfondo.frame = CGRectMake(self.nuvoleSfondo.frame.origin.x+1, self.nuvoleSfondo.frame.origin.y, self.nuvoleSfondo.frame.size.width, self.nuvoleSfondo.frame.size.height);
                    self.posX = scrollView.contentOffset.x;
    }
    else
        if (self.posX < scrollView.contentOffset.x-2)
        {
            self.nuvoleSfondo.frame = CGRectMake(self.nuvoleSfondo.frame.origin.x-1, self.nuvoleSfondo.frame.origin.y, self.nuvoleSfondo.frame.size.width, self.nuvoleSfondo.frame.size.height);
            self.posX = scrollView.contentOffset.x;
        }
}

@end
